#pragma once
#include"RBTree.h"
namespace m_std{

    template<class K, class V>
    class map{
        struct mapOfValue{
            const K& operator()(const pair<K, V>& data){
                return data.first;
            }
        };
    public:
        typedef typename RBTree<K, pair<K, V>, mapOfValue>::iterator iterator;
        iterator begin(){
            return _t.begin();
        }
        iterator end(){
            return _t.end();
        }
        pair<iterator, bool> insert(const pair<K, V> &val)
        {
            return _t.insert(val);
        }
        V& operator[](const K& key){
            pair<iterator, bool> ret = insert(make_pair(key, V()));
            return ret.first->second;
        }
    private:
        RBTree<K, pair<K, V>, mapOfValue> _t;
    };
    void mapTest(){
        map<int, int> m;
        map<int, int>::iterator it = m.begin();
        while (it != m.end())
        {
            cout << it->first << endl;
            ++it;
        }
        m.insert(make_pair(1, 5));
        m.insert(make_pair(3, 5));
        m.insert(make_pair(4, 5));
        m.insert(make_pair(7, 5));
        m.insert(make_pair(2, 5));

        it = m.begin();
        while(it != m.end()){
            cout << it->first << endl;
            ++it;
        }
    }
    void mapTest2(){
        string arr[] = {"苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉"};

        map<string, int> countMap;
        for (auto &str : arr)
        {
            // 1、str不在countMap中，插入pair(str, int()),然后在对返回次数++
            // 2、str在countMap中，返回value(次数)的引用，次数++;
            countMap[str]++;
        }

        map<string, int>::iterator it = countMap.begin();
        while (it != countMap.end())
        {
            cout << it->first << ":" << it->second << endl;
            ++it;
        }

        for (auto &kv : countMap)
        {
            cout << kv.first << ":" << kv.second << endl;
        }
    }
};